package com.esdk.utils;

import cn.hutool.core.io.IORuntimeException;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.esdk.esdk;
import lombok.NonNull;
import org.nutz.http.Http;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

public class EasyHttp{
	private static int Default_Read_Timeout=60000;//60秒
	private static int tryTimes=3;
	public static String get(@NonNull String url) {
		for(int i=0;i<tryTimes;i++){
			try{
				return HttpUtil.get(url);
			}catch(RuntimeException e){
				if(i<tryTimes-1)
					esdk.tool.sleep(1000*(i+1));
				else
					throw e;
			}
		}
		return null;
	}
	
	public static String get(@NonNull String url,Map map) {
		for(int i=0;i<tryTimes;i++){
			try{
				return HttpUtil.get(url,map,Default_Read_Timeout);
			}catch(RuntimeException e){
				if(i<tryTimes-1)
					esdk.tool.sleep(1000*(i+1));
				else
					throw e;
			}
		}
		return null;
	}

	public static String get(@NonNull String url,Map<String,Object> params,Map<String,Object> header) {
		HttpRequest postReq=HttpUtil.createGet(url);
		if(header!=null)
			header.forEach((k,v)->postReq.header(k,v instanceof List list?esdk.str.valueOf(list.getFirst()):esdk.str.valueOf(v)));
		postReq.form(params).setReadTimeout(Default_Read_Timeout);
		for(int i=0;i<tryTimes;i++){
			try{
				return postReq.execute().body();
			}catch(RuntimeException e){
				if(i<tryTimes-1)
					esdk.tool.sleep(1000*(i+1));
				else
					throw e;
			}
		}
		return null;
	}

	public static String post(@NonNull String url,Map<String,Object> params,Map<String,Object> header) {
		HttpRequest postReq=HttpUtil.createPost(url);
		if(header!=null)
			header.forEach((k,v)->postReq.header(k,v instanceof List list?esdk.str.valueOf(list.getFirst()):esdk.str.valueOf(v)));
		postReq.form(params).setReadTimeout(Default_Read_Timeout);
		for(int i=0;i<tryTimes;i++){
			try{
				return postReq.execute().body();
			}catch(RuntimeException e){
				if(i<tryTimes-1)
					esdk.tool.sleep(1000*(i+1));
				else
					throw e;
			}
		}
		return null;
	}

	public static String post(@NonNull String url,String body,Map<String,Object> header) {
		HttpRequest postReq=HttpUtil.createPost(url);
		if(header!=null)
			header.forEach((k,v)->postReq.header(k,v instanceof List list?esdk.str.valueOf(list.getFirst()):esdk.str.valueOf(v)));
		postReq.body(body).setReadTimeout(Default_Read_Timeout);
		for(int i=0;i<tryTimes;i++){
			try{
				return postReq.execute().body();
			}catch(RuntimeException e){
				if(i<tryTimes-1)
					esdk.tool.sleep(1000*(i+1));
				else
					throw e;
			}
		}
		return null;
	}

	public static String post(@NonNull String url,Map<String,Object> params) {
		return post(url,params,null);
	}

	public static String post(@NonNull String url,String body) {
		return post(url,body,null);
	}

	public static boolean wget(@NonNull String url,@NonNull File targetFile){
		InputStream is=Http.get(url).getStream();
		byte[] b=new byte[10240];
		int len=0;
		try{
			FileOutputStream out=new FileOutputStream(targetFile);
			while((len=is.read(b))!=-1){
				out.write(b,0,len);
			}
			is.close();
			out.close();
			return true;
		}catch(IOException e){
			throw new IORuntimeException(e);
		}
	}

	public static void main(String[] args){
		String res=EasyHttp.get("https://ebaijian.com/");
		esdk.tool.assertEquals(res.contains("百箭科技"));
	}

}
